package moa.learners.reservoir;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.ListIterator;


/**
 * This class stores the blacklisted sequences in a sorted list.
 * Each element of the list stores the sequence IDs sharing the
 * same blacklisting time.
 * 
 * That implementation allows a reasonable lookup time and a very
 * fast adding and removal time.
 * 
 * Every time the blacklist counters are decremented, the first
 * element is simply discarded and the others shifted, a new empty
 * one is inserted at the end. 
 */
public class SlicedBlackList implements AbstractBlackList {

	ArrayList<HashSet<String>> blackList;

	/**
	 * Creates an instance of SlicedBlackList
	 * 
	 * @param windowSize	Reservoir's sliding window size
	 */
	public SlicedBlackList(int windowSize) {
		blackList = new ArrayList<HashSet<String>>();
		for( int i = 0; i < windowSize; i++)
			blackList.add( new HashSet<String>());
	}

	public boolean contains( String seqNumber) {
		ListIterator<HashSet<String>> iteBl = blackList.listIterator();

		while( iteBl.hasNext()) {
			HashSet<String> setBl = iteBl.next();
			if( setBl.contains(seqNumber))
				return true;
		}

		return false;
	}

	public void decrement() {
		blackList.remove(0);
		blackList.add(new HashSet<String>());
	}

	public void add( String seqNumber, int windowSize) {
		blackList.get(windowSize-1).add(seqNumber);
	}

	public String inspect() {
		StringBuilder res = new StringBuilder( "[ ");
		for( int i = 1; i <= blackList.size(); i++) {
			res.append(i).append(" => ");
			Iterator<String> iteSet = blackList.get(i-1).iterator();
			while( iteSet.hasNext())
				res.append(iteSet.next()).append(",");
			res.replace( res.length()-1, res.length(), " ; ");
		}
		res.replace( res.length()-2, res.length(), " ]");
		return res.toString();
	}

}
